<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_documents_User extends m_users_documents_UserBase
{
	/**
	 * @return m_users_documents_User
	 */
	public static function createNew()
	{
		return new self();
	}
	
	/**
	 * @return m_users_documents_User
	 */
	public static function getInstanceByUniqueId($id)
	{
		$lang = f_core_Context::getInstance()->getLang();
		$cache = f_core_Cache::getInstance();
		
		$key = $cache->getDocumentCacheKey($id, $lang);
		
		if($cache->hasCache($key))
		{
			return $cache->getCacheContent($key);
		}
		
		$object = new self($id);
		
		$cache->putInCache($key, $object);
		
		return $object;
	}
	
	public function __toString()
	{
		return $this->getFirstname().' '.$this->getLastname();
	}
	
	public function canSlide()
	{
		return false;
	}
	
	public function getEditUrl($lang = null, $tabName = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}
		
		if(is_null($tabName))
		{
            $tabName = "properties";
		}

		return f_core_Routing::getUrl("userEdit", array("tabName" => $tabName, "docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}

	public function getRightUrl($lang = null, $subTabName = null)
	{
		if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}

		if(is_null($subTabName))
		{
			$tabName = "website";
		}

		return f_core_Routing::getUrl("userRightEdit", array("subTabName" => $subTabName, "docId" => $this->getUniqueId(), "docLang" => strtolower($lang)));
	}

    /**
     *  Check if the user has the right
     * @param <type> $rules
     * @return boolean
     */
    public function hasRight($rule)
    {
        $rights = m_rights_services_RightsService::getInstance()->getUserRights($this);
        return in_array($rule, $rights);
    }

    /**
     *  Check if the user has the right for a document
     * @param <type> $document
     * @param <type> $module
     * @param <type> $rule ('front', 'read', 'edit', 'add', 'delete')
     * @param <type> $type AND, OR
     * @return boolean
     */
    public function hasDocRight($document, $module, $rules, $type = "AND")
    {
        if(!is_array($rules))
        {
            $rules = array($rules);
        }

        if(!empty($rules))
        {
            $rights = m_rights_services_RightsService::getInstance()->getRightsFromDocument($this->getUniqueId(), $module, $document);

            if($type == "OR")
            {
                foreach($rules as $rule)
                {
                    if(!isset($rights[$rule]) || $rights[$rule] > 0)
                    {
                        return true;
                    }
                }

                return false;
            }
            else
            {
                foreach($rules as $rule)
                {
                    if(isset($rights[$rule]) && $rights[$rule] == 0)
                    {
                        return false;
                    }
                }
            }
        }

        return true;
    }
	
	public function getIcon()
	{
		return "";
	}

    public function belongsGroup($group)
    {
		$parents = f_relation_Manager::getInstance()->getParentsByDocType($this->getUniqueId(), "modules_users/group");

		foreach($parents as $parent)
		{
			if($parent->getUniqueId() == $group->getUniqueId())
			{
				return true;
			}
		}

		return false;
    }

    public function getGroupEditUrl($lang = null, $tabName = null)
    {
        if(is_null($lang))
		{
			$lang = f_core_Request::getInstance()->getParameter("docLang");
		}
        
        return $this->getDocumentParent()->getEditUrl($lang, $tabName);
    }
    
    public function setExtraParameter($key, $value)
    {
        $extraParams = unserialize($this->getExtraParams());
        if(!is_array($extraParams))
            $extraParams = Array();

        $extraParams[$key] = $value;
        $this->setExtraParams(serialize($extraParams));
        return $this;
    }

    public function getExtraParameter($key)
    {
        $extraParams = Array();
        $extraParams = unserialize($this->getExtraParams());

        if(isset($extraParams[$key]))
            return $extraParams[$key];
        else
            return null;
    }

    public function getBoSearchLabel()
    {
        if(!is_null($this->getFirstname()) && $this->getFirstname() != '') $res .= $this->getFirstname().' ';
        if(!is_null($this->getLastname()) && $this->getLastname() != '') $res .= $this->getLastname().' ';
        if(!is_null($this->getFirstname()) && $this->getFirstname() != '' && !is_null($this->getLastname()) && $this->getLastname() != '') $res .= '(';
        $res .= $this->getLabel();
        if(!is_null($this->getFirstname()) && $this->getFirstname() != '' && !is_null($this->getLastname()) && $this->getLastname() != '') $res .= ')';
        return $res;
    }

    public function getBoSearchIcon()
    {
        return m_medias_services_MediaService::getInstance()->getIconPath('user.png');
    }
}